/*****  Component, by Ben Bi <prince.bi@gmail.com>  created at: 12/23/2009 4:10:45 PM  *****/

/********   Notes:  *******
 *  Two tags need to be replaced with the module name, they are:
 *  weaveQcFormItem
 *  WeaveQcFormItem
 *  weave
 */

/*****  Component, by Ben Bi <prince.bi@gmail.com>  created at: 12/23/2009 4:10:45 PM  *****/

/********   Notes:  *******
 *  Two tags need to be replaced with the module name one by one, they are:
 *  weaveQcForm
 *  WeaveQcForm
 *  weave_qc_form
 */


/***** variables declartion section, begin *****/
var weaveQcFormItemInputMode = wacFormInputMode.add;
var weaveQcFormItemObj = {};
var weaveQcFormObj = {};

/***** variables declartion section, end *****/

/***** init section, begin *****/
$(document).ready(
    function() {
        initWeaveQcFormItemFormDialog();

        initWeaveQcFormItemForm();

        bindWeaveQcFormItemEvents();
    }
);

function bindWeaveQcFormItemEvents()
{
    $("#weaveQcFormItemFormDialog").bind('dialogopen', function(event, ui)
    {
        initWeaveQcFormItemFormData();
//        wacDebugLog("calling " + moduleName + ":" + wacModelObj.weaveQcFormItem.name);//
    });

    $("#weaveQcFormItemFormDialog").bind('dialogclose', function(event, ui)
    {
        $.validationEngine.closePrompt(".formError", true);
    });

    $("#weaveQcFormItem_btnSubmit").bind('click', function (e)
    {
        submitWeaveQcFormItemForm();
    });

    $("#weaveQcFormItem_btnClose").bind('click', function (e)
    {
        $("#weaveQcFormItemFormDialog").dialog('close');
    });

    // fix dialog div didnt remove bug, remove it by this way
    $('#appStorehouseRightPane').bind('tabsremove', function(event, ui) {
        if(ui.panel.id == "t14002")
        {
            $("div[aria-labelledby='ui-dialog-title-weaveQcFormItemFormDialog']").remove();
            $("#weaveQcFormItemFormDialog").remove();
        }
    });
}


function initWeaveQcFormItemFormDialog()
{
//    wacDebugLog("initWeaveQcFormItemFormDialog.");
    $("#weaveQcFormItemFormDialog").dialog({
            bgiframe: true,
            modal: true,
            width: 1060,
            height: 450,
            autoOpen: false,
            buttons: {},
            zIndex: 100
        });
}

function initWeaveQcFormItemForm()
{
    $("#weaveQcFormItemForm").validationEngine();

    $('.wacFormContent .left, .wacFormContent input, .wacFormContent textarea, .wacFormContent select').focus(function(){
        $(this).parents('.wacFormContent').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormContent').removeClass("wacFormOver");
    });

    $('.wacFormContentA .left, .wacFormContentA input, .wacFormContentA textarea, .wacFormContentA select').focus(function(){
        $(this).parents('.wacFormRow').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormRow').removeClass("wacFormOver");
    });


}

function initWeaveQcFormItemFormData()
{
    wacShowBlockUILoading("#weaveQcFormItemFormDialog");
    $('#weaveQcFormItem_spinner_id, #weaveQcFormItem_warp_density_unit_code, #weaveQcFormItem_filling_density_unit_code, #weaveQcFormItem_texture_unit_code').empty();

    var params = {
        "data_format" : 'json',
        "weave_qc_form_id": weaveQcFormObj.id
    };

    $.ajax({
        url: BASE_URL + "weaveQcFormItem/getFormData",
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            initWeaveQcFormItemFormDataCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function initWeaveQcFormItemFormDataCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       alert(jsonData.info.error_info);
   }
   else
    {
        var selectedIndex = 0;

        for(i=0;i<jsonData['items']['unit'].length;i++)
        {
            $('<option value="' + jsonData['items']['unit'][i].key +'">' + jsonData['items']['unit'][i].value +'</option>').appendTo("#weaveQcFormItem_warp_density_unit_code");
            $('<option value="' + jsonData['items']['unit'][i].key +'">' + jsonData['items']['unit'][i].value +'</option>').appendTo("#weaveQcFormItem_filling_density_unit_code");
            $('<option value="' + jsonData['items']['unit'][i].key +'">' + jsonData['items']['unit'][i].value +'</option>').appendTo("#weaveQcFormItem_texture_unit_code");
        }

        for(i=0;i<jsonData['items']['spinner'].length;i++)
        {
            $('<option value="' + jsonData['items']['spinner'][i].key +'">' + jsonData['items']['spinner'][i].value +'</option>').appendTo("#weaveQcFormItem_spinner_id");
        }
   }

   setupWeaveQcFormItemFormDefaultValues(jsonData['items']['default']);

   wacHideBlockUI("#weaveQcFormItemFormDialog");

//   wacDebugLog($.dump(jsonData));
}

function setupWeaveQcFormItemFormDefaultValues(defaultValueObj)
{
    $("#weaveQcFormItem_weave_qc_form_id").attr("value", weaveQcFormObj.id);
    $("#weaveQcFormItem_weave_qc_form_code").attr("innerHTML", weaveQcFormObj.code);
    $("#weaveQcFormItem_production_order_code").attr("innerHTML", weaveQcFormObj.production_order_code);
    $("#weaveQcFormItem_weave_order_code").attr("innerHTML", weaveQcFormObj.weave_order_code);
    $("#weaveQcFormItem_goods_category_name").attr("innerHTML", weaveQcFormObj.goods_category_name);

    if(weaveQcFormItemInputMode == wacFormInputMode.add)   // use default values
    {
        $("#weaveQcFormItem_id").attr("value", 0);
        $("#weaveQcFormItem_warp_density_unit_code option:[value='"+ defaultValueObj.warp_density_unit_code +"']").attr('selected', 'selected');
        $("#weaveQcFormItem_filling_density_unit_code option:[value='"+ defaultValueObj.filling_density_unit_code +"']").attr('selected', 'selected');
        $("#weaveQcFormItem_texture_unit_code option:[value='"+ defaultValueObj.texture_unit_code +"']").attr('selected', 'selected');
    }
    else  // use edit obj values
    {
        $("#weaveQcFormItem_id").attr("value", weaveQcFormItemObj.id);
        $("#weaveQcFormItem_weave_space").attr("value", weaveQcFormItemObj.weave_space);
        $("#weaveQcFormItem_cloth_space").attr("value", weaveQcFormItemObj.cloth_space);
        $("#weaveQcFormItem_quota").attr("value", weaveQcFormItemObj.quota);

        $("#weaveQcFormItem_warp_density").attr("value", weaveQcFormItemObj.warp_density);
        $("#weaveQcFormItem_filling_density").attr("value", weaveQcFormItemObj.filling_density);
        $("#weaveQcFormItem_goods_quantity").attr("value", weaveQcFormItemObj.goods_quantity);
        $("#weaveQcFormItem_texture").attr("value", weaveQcFormItemObj.texture);
        $("#weaveQcFormItem_filling_yarn_arrangement").attr("value", weaveQcFormItemObj.filling_yarn_arrangement);
        $("#weaveQcFormItem_warp_yarn_arrangement").attr("value", weaveQcFormItemObj.warp_yarn_arrangement);
        $("#weaveQcFormItem_beveled").attr("value", weaveQcFormItemObj.beveled);
        $("#weaveQcFormItem_weight").attr("value", weaveQcFormItemObj.weight);

        $("#weaveQcFormItem_spinner_id option:[value='"+ weaveQcFormItemObj.spinner_id +"']").attr('selected', 'selected');
        $("#weaveQcFormItem_warp_density_unit_code option:[value='"+ weaveQcFormItemObj.warp_density_unit_code +"']").attr('selected', 'selected');
        $("#weaveQcFormItem_filling_density_unit_code option:[value='"+ weaveQcFormItemObj.filling_density_unit_code +"']").attr('selected', 'selected');
        $("#weaveQcFormItem_texture_unit_code option:[value='"+ weaveQcFormItemObj.texture_unit_code +"']").attr('selected', 'selected');
    }

//wacDebugLog($.dump(weaveQcFormItemObj));
//    wacDebugLog($.dump(weaveQcFormObj));
//    wacDebugLog($.dump(defaultValueObj));
//    wacDebugLog("weaveQcFormItemInputMode: " + weaveQcFormItemInputMode);

}
/***** init section, end *****/


/**************  interaction section, begin  ***************/

function weaveQcFormItemOpenModuleForm(moduleFormName, moduleName, inputMode, rowid, parentId)
{
    weaveQcFormItemInputMode = inputMode;

    if(parentId === undefined || parentId==0){
        weaveQcFormObj = {};
    }
    else
    {
        weaveQcFormObj = $("#weaveQcFormList").getRowData(parentId);
    }

    if(rowid === undefined || rowid==0){
        weaveQcFormItemObj = {};
    }
    else
    {
        weaveQcFormItemObj = $("#weaveQcFormList_" + weaveQcFormObj.id + "_t").getRowData(rowid);
    }

    $('#'+moduleFormName).dialog('open');
}

function validateWeaveQcFormItemForm()
{
    var validateFlag = true;

    if (!$("#weaveQcFormItemForm").validationEngine({returnIsValid:true}))
    {
        validateFlag = false;
        return validateFlag;
    }

    return validateFlag;
}

function submitWeaveQcFormItemForm()
{
    if(!validateWeaveQcFormItemForm()){
        return;
    }

    wacShowBlockUILoading("#weaveQcFormItemFormDialog", "处理中...");

    var extraParams = "data_format=json";
    var submitUrl;

    if(weaveQcFormItemInputMode == wacFormInputMode.add){
        submitUrl = BASE_URL + "weaveQcFormItem/add";
    }
    else{
        submitUrl = BASE_URL + "weaveQcFormItem/edit";
    }


    $.ajax({
        url: submitUrl,
//        url: BASE_URL + "test/ajaxTest" ,
        global: true,
        type: "GET",
        data: $("#weaveQcFormItemForm").serialize() + "&" + extraParams,
        dataType: "json",
        success: function(jsonData){
            submitWeaveQcFormItemFormCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function submitWeaveQcFormItemFormCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       wacShowTips(jsonData.info.error_info);
   }
   else
   {
       wacShowTips(jsonData.info.tips);
       $("#weaveQcFormList_" + weaveQcFormObj.id + "_t").trigger("reloadGrid");
   }

   wacHideBlockUI("#weaveQcFormItemFormDialog");

//   wacDebugLog($.dump(jsonData));
}

/**************  interaction section, end  ***************/